<template>
  <el-row class="home" :gutter="20">
    <el-col :span="8" style="margin-top: 20px">
      <el-card shadow="hover" display="flex">
        <div class="user">
          <img src="../../assets/image/touxiang.jpeg" alt="" />
          <div class="user-info">
            <p class="name">{{ user.name }}</p>
            <p class="role">{{ user.role }}</p>
          </div>
        </div>
        <div class="login-info">
          <p>
            上次登录时间: <span>{{ user.lastLoginTime }}</span>
          </p>
          <p>
            上次登录地点: <span>{{ user.lastLoginAddress }}</span>
          </p>
        </div>
      </el-card>
      <el-card
        display="flex"
        shadow="hover"
        style="margin-top: 20px; height: 400px"
      >
        <el-table :data="tableData">
          <el-table-column
            v-for="(item, index) in tableLabel"
            :key="index"
            :label="item"
            :prop="index"
          >
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
    <el-col :span="16" style="margin-top: 20px">
      <div class="num">
        <el-card
          :body-style="{ display: 'flex', padding: 0 }"
          v-for="item in countData"
          :key="item.name"
        >
          <component
            class="icons"
            :is="item.icon"
            :style="{ background: item.color }"
          >
          </component>
          <div class="details">
            <p class="num">{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <el-card style="height: 280px">
        <div ref="echart" style="height: 280px"></div>
      </el-card>
      <div class="graph">
        <el-card class="echart" style="height: 260px">
          <div ref="userEchart" style="height: 200px"></div>
        </el-card>
        <el-card class="echart" style="height: 260px">
          <div ref="videoEchart" style="height: 200px"></div>
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script setup>
import { ref } from "vue";
import { reactive } from "vue";
import { onMounted, watch } from "vue";
import { getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import * as echarts from "echarts";
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const tableLabel = ref({
  name: "商品名称",
  todayBuy: "今日销售",
  monthBuy: "本月销售",
  totalBuy: "总销售",
});
const countData = ref([]);
const user = ref({
  name: "Admin",
  role: "管理员",
  lastLoginTime: "2023-6-8",
  lastLoginAddress: "龙岩",
});

const getTableList = async () => {
  let res = await proxy.$api.getTableData();
  tableData.value = res.tableData;
};
const getCountData = async () => {
  let res = await proxy.$api.getCountData();
  countData.value = res;
};
const router = useRouter();
onMounted(() => {
  getTableList();
  getCountData();
  getChartData();
});

//关于echarts 表格的渲染部分
let xOpthons = reactive({
  //图例文字颜色
  textStyle: {
    color: "#333",
  },
  grid: {
    left: "20%",
  },
  //提示框
  tooltip: {
    trigger: "axis",
  },
  xAxis: {
    type: "category", //类目轴
    data: [],
    axisLine: {
      lineStyle: {
        color: "#17b3a3",
      },
    },
    axisLabel: {
      interval: 0, //横轴信息全部显示
      color: "#333",
    },
  },
  yAxis: [
    {
      type: "value",
      axisLine: {
        lineStyle: {
          color: "#17b3a3",
        },
      },
    },
  ],
  color: ["#2ec7c9", "#b6a2de", "#5ab1ef", "#ffb980", "#d87a80", "#8d98b3"],
  series: [],
});
let pieOpthons = reactive({
  tooltio: {
    trigger: "item",
  },
  color: [
    "RGB(078, 098, 171)",
    "RGB(070, 158, 180)",
    "RGB(135, 207, 164)",
    "RGB(203, 233, 157)",
    "RGB(245, 251, 177)",
    "RGB(254, 232, 154)",
    "RGB(253, 185, 106)",
  ],
  series: [],
});
let orderData = reactive({
  xData: [],
  series: [],
});
let userData = reactive({
  xData: [],
  series: [],
});
let videoData = reactive({
  series: [],
});

//获取echarts数据
const getChartData = async () => {
  let result = await proxy.$api.getChartData();
  console.log(result);
  let res = result.orderData;
  let userRes = result.userData;
  let videoRes = result.videoData;
  orderData.xData = res.date;
  const keyArray = Object.keys(res.data[0]);
  const series = [];
  keyArray.forEach((key) => {
    series.push({
      name: key,
      data: res.data.map((i) => i[key]),
      type: "line",
    });
  });
  orderData.series = series;
  xOpthons.xAxis.data = orderData.xData;
  xOpthons.series = orderData.series;
  //userData进行渲染
  let hEcharts = echarts.init(proxy.$refs["echart"]);
  hEcharts.setOption(xOpthons);
  //柱状图进行渲染
  userData.xData = userRes.map((i) => i.date);
  userData.series = [
    {
      name: "新增用户",
      data: userRes.map((i) => i.new),
      type: "bar",
    },
    {
      name: "活跃用户",
      data: userRes.map((i) => i.active),
      type: "bar",
    },
  ];
  xOpthons.xAxis.data = userData.xData;
  xOpthons.series = userData.series;
  let uEcharts = echarts.init(proxy.$refs["userEchart"]);
  uEcharts.setOption(xOpthons);
  //饼状图渲染
  videoData.series = [
    {
      data: videoRes.map((i) => {
        return {
          name: i.name,
          value: i.value,
        };
      }),
      type: "pie",
    },
  ];
  pieOpthons.series = videoData.series;
  let vEcharts = echarts.init(proxy.$refs["videoEchart"]);
  vEcharts.setOption(pieOpthons);
};
</script>

<style lang="less" scoped>
.user {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 1px solid #eee;
  margin-bottom: 5%;
  .user-info {
    .name {
      font-size: 20px;
      font-weight: bold;
    }
    .role {
      font-size: 14px;
      color: #999;
    }
  }
  img {
    width: 100px;
    height: 100px;
    border-radius: 50%;
    margin-right: 10%;
  }
}
.login-info {
  p {
    margin-bottom: 10px;
    font-size: 16px;
    span {
      color: #999;
      font-size: 14px;
      margin-left: 20%;
    }
  }
}
.num {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  .el-card {
    width: 32%;
    margin-bottom: 3%;
  }
  .icons {
    width: 20%;
    font-size: 2rem;
    text-align: center;
    line-height: 50%;
    color: white;
  }
  .details {
    margin-left: 10px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    .num {
      font-size: 2rem;
      margin-bottom: 10px;
    }
    .txt {
      font-size: 0.8rem;
      text-align: center;
      color: #999;
    }
  }
}
.graph {
  display: flex;
  justify-content: space-between;
  margin-top: 3%;
  .echart {
    width: 48%;
    height: 400px;
  }
}
</style>
